package com.wu.dms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.common.convertion.errorcode.BaseErrorCode;
import com.wu.common.convertion.exception.ServiceException;
import com.wu.dms.dao.entity.Article;
import com.wu.dms.dao.mapper.ArticleMapper;
import com.wu.dms.service.ArticleService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Override
    public String uploadImage(MultipartFile imageFile) {
        String fileName = System.currentTimeMillis() + imageFile.getOriginalFilename();
        //路径 他这个会根据你的系统获取对应的文件分隔符
        String filePath = "F:\\java\\nginx-music-client\\html" + FileSystems.getDefault().getSeparator() + "img" + FileSystems.getDefault().getSeparator() + "content";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
        File dest = new File(filePath + FileSystems.getDefault().getSeparator() + fileName);
        String imgPath = "/img/content/" + fileName;
        try {
            imageFile.transferTo(dest);
        } catch (IOException e) {
            log.error(e.toString());
            throw new ServiceException(BaseErrorCode.SERVICE_TIMEOUT_ERROR);
        }

        return imgPath;
    }

    @Override
    public List<Article> findAll() {
        return null;
    }
}
